package com.example.dell.fastshopping.loginRegist.moldle;

import android.arch.lifecycle.LiveData;
import android.arch.lifecycle.MutableLiveData;
import android.arch.lifecycle.ViewModel;

import com.example.dell.fastshopping.DuoBao.GoodsIntroduceFragment;
import com.example.dell.fastshopping.network.ApiService;
import com.example.dell.fastshopping.network.NetCallback;
import com.example.dell.fastshopping.network.RetrofitClient;
import com.example.dell.fastshopping.network.http.BaseResponse;
import com.example.dell.fastshopping.user.entity.UserInfo;
import com.example.dell.fastshopping.utils.DESUtils;
import com.example.dell.fastshopping.utils.KLog;
import com.example.dell.fastshopping.utils.RegexUtils;
import com.example.dell.fastshopping.utils.SPDao;
import com.example.dell.fastshopping.utils.SPUtils;
import com.example.dell.fastshopping.utils.ToastUtils;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.util.HashMap;

import io.reactivex.Observer;
import io.reactivex.functions.Consumer;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.http.Query;

/**
 * Created by ${YSH} on 2019/4/24.
 * 描述        ${TODO}
 * <p>
 * 更新者       $author$
 * 更新时间     $Data$
 * 描述         ${TODO}
 */
public class LoginRegistResetMoldle extends ViewModel {
    private ApiService apiService = RetrofitClient.getInstance().create(ApiService.class);
    private MutableLiveData<Boolean> startTiem = new MutableLiveData();
    private MutableLiveData<Boolean> registstartTiem = new MutableLiveData();
    private MutableLiveData<Boolean> resetstartTiem = new MutableLiveData();
    private MutableLiveData<Boolean> loginSuccess = new MutableLiveData();
    private MutableLiveData<Boolean> RegistSuccess = new MutableLiveData();
    private MutableLiveData<Boolean> resetSuccess = new MutableLiveData();


    public MutableLiveData<Boolean> getLoginCode(String phone) {
        if (RegexUtils.isMobileExact(phone)) {
            RetrofitClient.execute(apiService.loginCode(phone), new NetCallback<BaseResponse>() {
                @Override
                public void netCallback(BaseResponse response) {
                    if (response.getCode() == 200) {
                        startTiem.setValue(true);
                    }
                }
            });
        } else {
            ToastUtils.showShort("手机号码不正确");
        }
        return startTiem;
    }

    public MutableLiveData<Boolean> login(String phone, String code, String pws) {
        String encode = "";
        if (!RegexUtils.isMobileExact(phone.trim())) {
            ToastUtils.showShort("手机号码不正确");
            return loginSuccess;
        }

        if (RegexUtils.isEmpty(code) && RegexUtils.isEmpty(pws)) {
            ToastUtils.showShort("请填写密码或验证码");
            return loginSuccess;
        }

        if (!RegexUtils.isEmpty(pws) && !RegexUtils.isPws(pws)) {
            ToastUtils.showShort("密码格式不正确");
            return loginSuccess;
        }
        if (!RegexUtils.isEmpty(pws)) {
            encode = DESUtils.utilEncode(pws);
        }

        final RequestBody requestBody = new LoginRequestRequest(phone, encode, code, "").getRequestBody();
        RetrofitClient.execute(apiService.login(requestBody), new NetCallback<BaseResponse<UserInfo>>() {
            @Override
            public void netCallback(BaseResponse<UserInfo> response) {
                if (response.isSuccess()) {
//                 TODO:   response.getData().getList();
                    loginSuccess.setValue(true);
                    SPUtils.getInstance().put("isFirst", false);
                    SPDao.saveUserId(response.getData().getId());
                }
            }
        });
        return loginSuccess;
    }

    public LiveData<Boolean> getRegistCode(String phone) {
        if (RegexUtils.isMobileExact(phone)) {
            RetrofitClient.execute(apiService.registCode(phone), new NetCallback() {
                @Override
                public void netCallback(Object o) {
                    registstartTiem.setValue(true);
                }
            });
        } else {
            ToastUtils.showShort("手机号码不正确");
        }
        return registstartTiem;
    }

    public LiveData<Boolean> regist(String phone, String pws, String code, String inviteCode) {

        if (!RegexUtils.isMobileExact(phone)) {
            ToastUtils.showShort("手机号码不正确");
            return RegistSuccess;
        }

        if (RegexUtils.isEmpty(code) && RegexUtils.isEmpty(pws)) {
            ToastUtils.showShort("请填写密码或验证码");
            return RegistSuccess;
        }

        if (!RegexUtils.isEmpty(pws) && !RegexUtils.isPws(pws)) {
            ToastUtils.showShort("密码格式不正确");
            return RegistSuccess;
        }

        String encode = DESUtils.utilEncode(pws);
        if (inviteCode.isEmpty()) {
            ToastUtils.showShort("请填写邀请码");
            return RegistSuccess;
        }

        RequestBody requestBody = new LoginRequestRequest(phone, encode, code, inviteCode).getRequestBody();
        RetrofitClient.execute(apiService.regist(requestBody), new NetCallback<BaseResponse>() {
            @Override
            public void netCallback(BaseResponse response) {
                if (response.getCode() == 200) {
                    RegistSuccess.setValue(true);
                }
            }
        });
        return RegistSuccess;
    }

    public LiveData<Boolean> getResetCode(String phone) {

        if (RegexUtils.isMobileExact(phone)) {
            RetrofitClient.execute(apiService.resetCode(phone), new NetCallback<BaseResponse>() {
                @Override
                public void netCallback(BaseResponse response) {
                    if (response.getCode() == 200) {
                        resetstartTiem.setValue(true);
                    }
                }
            });
        } else {
            ToastUtils.showShort("手机号码不正确");
        }
        return resetstartTiem;
    }

    public LiveData<Boolean> reset(String phone, String pws, String code) {

        if (!RegexUtils.isMobileExact(phone)) {
            ToastUtils.showShort("手机号码不正确");
            return resetSuccess;
        }
        if (RegexUtils.isEmpty(code) && RegexUtils.isEmpty(pws)) {
            ToastUtils.showShort("请填写密码或验证码");
            return resetSuccess;
        }
        if (!RegexUtils.isEmpty(pws) && !RegexUtils.isPws(pws)) {
            ToastUtils.showShort("密码格式不正确");
            return resetSuccess;
        }
        String encode = DESUtils.utilEncode(pws);

        RequestBody requestBody = new LoginRequestRequest(phone, encode, code, "").getRequestBody();
        RetrofitClient.execute(apiService.reset(requestBody), new NetCallback<BaseResponse>() {
            @Override
            public void netCallback(BaseResponse response) {
                if (response.getCode() == 200) {
                    resetSuccess.setValue(true);
                }
            }
        });
        return resetSuccess;
    }

    private class LoginRequestRequest {
        private final String phone;
        private final String pwd;
        private final String shortMessageCode;
        private final String invitationCode;
        private final String code;
        private final String newPwd;

        public LoginRequestRequest(String phone, String encode, String code, String inviteCode) {
            this.phone = phone;
            if (encode.isEmpty()) {
                this.pwd = null;
                this.newPwd = null;
            } else {
                this.pwd = encode;
                this.newPwd = encode;
            }
            this.shortMessageCode = code;
            this.code = code;
            this.invitationCode = inviteCode;
        }

        public String getJsonString() {
            Gson gson = new Gson();
            String s = gson.toJson(this);
            return s;
        }

        public RequestBody getRequestBody() {
            RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), getJsonString());
            return requestBody;
        }
    }
}
